<template>
    <el-dialog title="选择菜单" destroy-on-close append-to-body :visible.sync="visible">
        <div class="ec-dailog-body">

            <div style="width:100%">
                <tree-transfer :title="title" ref="tree" :from_data='fromData' :to_data='toData' @addBtn='add'
                    @removeBtn='remove' :mode='mode' height='540px' filter openAll arrayToTree :check-strictly="true">
                </tree-transfer>
            </div>

        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="submit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import treeTransfer from 'el-tree-transfer' // 引入
    import * as usergroupService from '@/api/upms/usergroup'
    import * as orgService from '@/api/upms/org'
    import * as userroleService from '@/api/upms/userrole'
    import * as userUsergroupService from '@/api/upms/userusergroup'
    import * as userService from '@/api/upms/user'

    import * as menuService from "@/api/upms/menu";
    import * as respModuleService from '@/api/upms/respmodule'
    import * as responsibilityService from '@/api/upms/responsibility'
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    pkOrgs: '',
                    pkResponsibility: '',
                    ids: []
                },
                title: ['未选择功能节点列表', '已选择功能节点列表'],
                mode: "transfer",
                usersData: [],
                fromData: [],
                toData: [],
                ids: [],
                orgsoptions: [],
                filterMethod(query, item) {
                    return item.name.indexOf(query) > -1;
                }
            }
        },
        components: {
            treeTransfer
        }, // 注册
        methods: {
            init(id) {
                this.visible = true
                this.toData = []
                this.ids = []
                this.dataForm.pkResponsibility = id;
                this.$nextTick(() => {
                    if (this.dataForm.pkResponsibility) {
                        // 加载已分配的模块
                        respModuleService.getRespUnSelectedModule(this.dataForm.pkResponsibility).then(
                            data => {
                                this.fromData = data
                                // console.table('this.fromData',this.fromData)
                            })
                        // 加载已分配的
                        respModuleService.getRespSelectedModule(this.dataForm.pkResponsibility).then(
                            data => {
                                this.toData = data
                                // 初始化选中的节点
                                this.getIdByTree(data, this.ids)
                            })
                    }
                })
            },
            // 获取树id节点集合
            getIdByTree(treeList, ids) {
                treeList.forEach((item, index) => {
                    if (item.children) {
                        getIdByTree(item.children)
                    } else {
                        ids.push(item.id)
                    }
                })
            },
            submit() {
                this.dataForm.ids = []
                this.dataForm.ids = this.ids
                respModuleService.addrespmodule(this.dataForm).then(data => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                            this.visible = false
                            this.$emit('refreshDataList')
                        }
                    })
                })

            },
            getTreeProp(arr, key) {
                let res = []
                for (var value of arr) {
                    if (value.children) {
                        this.getTreeProp(value.children)
                    }
                    res.push(value[key])
                }
                return res;
            },
            refreshUser() {},
            // 监听穿梭框组件添加
            add(fromData, toData, obj) {
                let ids = obj.keys.concat(obj.harfKeys)
                this.setIds('add', ids)
            },
            // 监听穿梭框组件移除
            remove(fromData, toData, obj) {
                let ids = obj.keys
                this.setIds('remove', ids)
            },
            // ids数组操作
            setIds(type, objs) {
                if (type === 'add') {
                    // 数据合并去重
                    objs.forEach(item => {
                        if (item !== null) {
                            this.ids.push(item)
                        }
                    })
                    this.ids = Array.from(new Set(this.ids))
                }
                if (type === 'remove') {
                    this.arrSub(this.ids, objs)
                }
            },
            // 数组相减
            arrSub(a, b) {
                for (var i = 0; i < b.length; i++) {
                    for (var j = 0; j < a.length; j++) {
                        if (a[j] == b[i]) {
                            a.splice(j, 1);
                            j = j - 1;
                        }
                    }
                }
                return a;
            }
        }
    }
</script>